<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo 
 *@link www.zeevin.com
 */

class Utils {

	public static function getAge($birth=2000){
		$current_year = date('Y');
		$birth = substr($birth,0,4);
		return ($current_year>$birth)?($current_year-$birth):'';
	}

	public static function getBirth($age=18){
		$current_year = date('Y');
		$birth = $current_year-$age;
		return ($age>=18)?($birth.'-'.date('m-d')):'';
	}

	public static function getPAth($ctime,$name){
		if($ctime){
			$tmp = str_replace('-','',substr($ctime, 0,7));

			return 'attachment/'.$tmp.'/'.$name;
		}
	}

	public static function getAvatar($avatar){
		$file = 'attachment/avatar/'.$avatar;
		if($avatar && file_exists($file)){
			return $file;
		}else{
			return '';
		}
	}

	public static function genAgeSelect(){
		
		$select = array_combine(range(80,18),range(80,18));
		// $select['']='不限';
		return array_reverse($select,true);

	}

	public static function genHeightSelect(){
		
		$select = array_combine(range(210,130),range(210,130));
		// $select['']='不限';
		return array_reverse($select,true);

	}

	public static function genThumb($imgPath=false,$x,$y,$w,$h){
		// echo $x,',',$y,',',$w,',',$h;
		if($imgPath){
			$type = CFileHelper::getExtension($imgPath);
			$newim = imagecreatetruecolor(100,100);
			$newpath = dirname($imgPath).'/thumb_'.basename($imgPath);
			switch ($type) {
				case in_array($type,array('jpg', 'jpeg')):
					$im = imagecreatefromjpeg($imgPath);
					imagecopyresampled($newim, $im, 0, 0, $x, $y, 100,100,$w, $h);
					return imagejpeg($newim,$newpath);
				break;
				case 'gif':
					$im = imagecreatefromgif($imgPath);	
					imagecopyresampled($newim, $im, 0, 0, $x, $y, 100,100,$w, $h);
					return imagegif($im,$newpath);			
				break;
				case 'bmp':
					$im = imagecreatefromwbmp($imgPath);
					imagecopyresampled($newim, $im, 0, 0, $x, $y, 100,100,$w, $h);
					return imagewbmp($im,$newpath);
				break;
				case 'png':
					$im = imagecreatefrompng($imgPath);
					imagecopyresampled($newim, $im, 0, 0, $x, $y, 100,100,$w, $h);
					return imagepng($im,$newpath);
				break;
				default:
					return false;
				break;
			}
			
			return false;
		}
		return false;
	}
	//将用户上传的原始图片切割成适合的大小
	public static function resizeAvatar($imgPath=false,$x=0,$y=0,$w=180,$h=232){
		// echo $x,',',$y,',',$w,',',$h;
		if($imgPath){
			$type = CFileHelper::getExtension($imgPath);
			$newim = imagecreatetruecolor($w,$h);
			$tmp_size = getimagesize($imgPath);
			switch ($type) {
				case in_array($type,array('jpg', 'jpeg')):
					$im = imagecreatefromjpeg($imgPath);
					imagecopyresampled($newim, $im, 0, 0, $x, $y, $w,$h,$tmp_size[0], $tmp_size[1]);
					imagedestroy($im);
					return imagejpeg($newim,$imgPath);
				break;
				case 'gif':
					$im = imagecreatefromgif($imgPath);	
					imagecopy($newim, $im, 0, 0, $x, $y, $w, $h);
					return imagegif($im,$newpath);			
				break;
				case 'bmp':
					$im = imagecreatefromwbmp($imgPath);
					imagecopy($newim, $im, 0, 0, $x, $y, $w, $h);
					return imagewbmp($im,$newpath);
				break;
				case 'png':
					$im = imagecreatefrompng($imgPath);
					imagecopy($newim, $im, 0, 0, $x, $y, $w, $h);
					return imagepng($im,$newpath);
				break;
				default:
					return false;
				break;
			}
			
			return false;
		}
		return false;
	}
	

}